﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Megaman
{
    /// <summary>
    /// Controls playback of an Animation.
    /// </summary>
    struct AnimationPlayback<StateName>
    {
        /// <summary>
        /// Gets the animation which is currently playing.
        /// </summary>
        public Animation Animation
        {
            get { return animation; }
        }
        Animation animation;

        /// <summary>
        /// Gets the index of the current frame in the animation.
        /// </summary>
        public int FrameIndex
        {
            get { return frameIndex; }
        }
        int frameIndex;

        /// <summary>
        /// The amount of time in seconds that the current frame has been shown for.
        /// </summary>
        private float time;


        public StateName State
        {
            get { return state; }
        }
        StateName state;

        /// <summary>
        /// Gets a texture origin at the bottom center of each frame.
        /// </summary>

        public Vector2 Origin()
        {
            return animation.Origin;
        }

        public Rectangle LocalBounds()
        {
            return animation.localBounds;
        }
        /// <summary>
        /// Begins or continues playback of an animation.
        /// </summary>
        public void PlayAnimation(Animation animation, StateName name)
        {
            // If this animation is already running, do not restart it.
            if (Animation == animation)
                return;

            this.state = name;
            this.animation = animation;
            if (animation != null)
            {
                // Start the new animation.
                this.frameIndex = animation.startingFrame;
                this.time = 0.0f;
            }
        }

        /// <summary>
        /// Advances the time position and draws the current frame of the animation.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects)
        {
            if (Animation == null)
            {
              throw new NotSupportedException("No animation is currently playing.");
                
            }

            // Process passing time.
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;


            // Advance the frame index; looping or clamping as appropriate.

            frameIndex = (int)(time * Animation.fps);
            if (Animation.isLooping)
            {
                frameIndex %= Animation.frameCount;
            }
            else
                frameIndex = (int)MathHelper.Min((float)frameIndex, (float)(Animation.frameCount - 1));



            // Calculate the source rectangle of the current frame.
            Rectangle source = Animation.SourceRectangle(frameIndex);

            // Draw the current frame.
            spriteBatch.Draw(Animation.getTexture(), position, source, Color.White, 0.0f, Animation.Origin, 1.0f, spriteEffects, 0.0f);
        }

        /// <summary>
        /// Advances the time position and draws the current frame of the animation.
        /// </summary>
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, float rotation, SpriteEffects spriteEffects)
        {
            if (Animation == null)
            {
               throw new NotSupportedException("No animation is currently playing.");

            }

            // Process passing time.
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;


            // Advance the frame index; looping or clamping as appropriate.

            frameIndex = (int)(time * Animation.fps);
            if (Animation.isLooping)
            {
                frameIndex %= Animation.frameCount;
            }
            else
                frameIndex = (int)MathHelper.Min((float)frameIndex, (float)(Animation.frameCount - 1));



            // Calculate the source rectangle of the current frame.
            Rectangle source = Animation.SourceRectangle(frameIndex);

            // Draw the current frame.
            spriteBatch.Draw(Animation.getTexture(), position, source, Color.White, rotation, Animation.Origin, 1.0f, spriteEffects, 0.0f);
        }
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, float rotation, SpriteEffects spriteEffects, Color color)
        {
            if (Animation == null)
            {
                throw new NotSupportedException("No animation is currently playing.");

            }

            // Process passing time.
            time += (float)gameTime.ElapsedGameTime.TotalSeconds;


            // Advance the frame index; looping or clamping as appropriate.

            frameIndex = (int)(time * Animation.fps);
            if (Animation.isLooping)
            {
                frameIndex %= Animation.frameCount;
            }
            else
                frameIndex = (int)MathHelper.Min((float)frameIndex, (float)(Animation.frameCount - 1));



            // Calculate the source rectangle of the current frame.
            Rectangle source = Animation.SourceRectangle(frameIndex);

            // Draw the current frame.
            spriteBatch.Draw(Animation.getTexture(), position, source, color, rotation, Animation.Origin, 1.0f, spriteEffects, 0.0f);
        }
    }
}
